﻿/*
 *  Source code courtesy of the desktopWeb.CodePlex.com community project. See MS-PL license on Codeplex.com. 
 *  This is beta code not intended for a production application.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Documents;
using MyData.ExtensionMethods;

namespace MyData.Workflow.Designer
{
  public partial class DictionaryEditorForm : Form
  {

    private ExpressionEditorUserControl _expressionEditor;

    public DictionaryEditorForm(ExpressionEditorUserControl expressionEditor)
    {
      InitializeComponent();

      _expressionEditor = expressionEditor;
    }

    //Dictionary string template
    private void applyButton_Click(object sender, EventArgs e)
    {
      _expressionEditor.expressionText.DocumentText("New System.Collections.Generic.Dictionary(Of String, Object) From{" + valueTextBox.Text + "}");

      this.Close();
    }

    //Build Dictionary string
    private void DictionaryEditorForm_Load(object sender, EventArgs e)
    {
      StringBuilder sb = new StringBuilder();
      string expression = string.Empty;
      string[] expressionParse = _expressionEditor.expressionText.DocumentText().Split(new char[] { '{' });

      
      for(int i = 2; i < expressionParse.Length; i++)
        sb.Append("{" + expressionParse[i]);
      
      expression = sb.ToString();
      //Next: Use TypeConverter
      if (expression.Length > 3)
        valueTextBox.Text = expression.Substring(0, expression.Length - 3);
    }

    private void cancelButton_Click(object sender, EventArgs e)
    {
      this.Close();
    }
  }
}
